What is eth-rpc-errors?
The eth-rpc-errors package provides utilities for creating and managing Ethereum JSON-RPC errors. It helps standardize error handling in Ethereum-related applications by providing predefined error codes and messages.
What are eth-rpc-errors's main functionalities?
Creating Custom Errors
This feature allows you to create custom errors with specific codes, messages, and additional data. It is useful for defining application-specific error conditions.
const { ethErrors } = require('eth-rpc-errors');
const customError = ethErrors.rpc.custom({
code: 1234,
message: 'Custom error message',
data: { additional: 'info' }
});
console.log(customError);
Predefined Errors
The package provides predefined errors for common JSON-RPC error conditions, such as invalid parameters, method not found, and internal errors. This helps standardize error responses across different applications.
const { ethErrors } = require('eth-rpc-errors');
const invalidParamsError = ethErrors.rpc.invalidParams('Invalid parameters provided');
console.log(invalidParamsError);
Error Serialization
This feature allows you to serialize JavaScript errors into a format that can be easily transmitted over JSON-RPC. It ensures that error information is consistently formatted.
const { serializeError } = require('eth-rpc-errors');
const error = new Error('Something went wrong');
const serializedError = serializeError(error);
console.log(serializedError);
Other packages similar to eth-rpc-errors
json-rpc-error
The json-rpc-error package provides utilities for creating and managing JSON-RPC errors. It offers similar functionality to eth-rpc-errors but is not specific to Ethereum. It can be used in any JSON-RPC based application.
jsonrpc-lite
The jsonrpc-lite package is a lightweight JSON-RPC 2.0 and 1.0 implementation. It includes error handling utilities similar to eth-rpc-errors but also provides full JSON-RPC request and response handling capabilities.
json-rpc-protocol
The json-rpc-protocol package provides a complete implementation of the JSON-RPC 2.0 protocol, including error handling. It offers a more comprehensive solution compared to eth-rpc-errors, which focuses solely on error management.
eth-rpc-errors
Ethereum RPC errors, including for
Ethereum JSON RPC
and
Ethereum Provider,
and making unknown errors compliant with either spec.
Basic Usage
import { ethErrors } from 'eth-rpc-errors'
throw ethErrors.provider.unauthorized()
throw ethErrors.provider.unauthorized('my custom message')
Supported Errors
- Ethereum JSON RPC
- Ethereum Provider errors
Usage
Installation: npm install eth-rpc-errors
or yarn add eth-rpc-errors
import
or require
as normal (no default export).
Errors API
import { ethErrors } from 'eth-rpc-errors'
response.error = ethErrors.rpc.methodNotFound({
message: optionalCustomMessage, data: optionalData
})
response.error = ethErrors.provider.unauthorized({
message: optionalCustomMessage, data: optionalData
})
response.error = ethErrors.provider.unauthorized(customMessage)
response.error = ethErrors.provider.unauthorized()
response.error = ethErrors.provider.unauthorized({})
response.error = ethErrors.rpc.server({
code: -32031
})
response.error = ethErrors.provider.custom({
code: 1001, message: 'foo'
})
Parsing Unknown Errors
import { serializeError } from 'eth-rpc-errors'
response.error = serializeError(maybeAnError)
const fallbackError = { code: 4999, message: 'My custom error.' }
response.error = serializeError(maybeAnError, fallbackError)
{
code: -32603,
message: 'Internal JSON-RPC error.'
}
Other Exports
import {
IEthErrors, IEthereumRpcError, IEthereumProviderError, ISerializeError,
IErrorOptions, IRpcServerErrorOptions, IProviderCustomErrorOptions
} from 'eth-rpc-errors/@types'
import { EthereumRpcError, EthereumProviderError } from 'eth-rpc-errors'
import { getMessageFromCode, ERROR_CODES } from 'eth-rpc-errors'
const message1 = getMessageFromCode(someCode)
const message2 = getMessageFromCode(someCode, myFallback)
const message3 = getMessageFromCode(someCode, null)
const code1 = ERROR_CODES.rpc.parse
const code2 = ERROR_CODES.provider.userRejectedRequest
const message4 = getMessageFromCode(code1)
const message5 = getMessageFromCode(code2)
License
MIT